<?php
class ModelSalePromotion extends Model {
	
	public function addPromotion($data = array()) {
		$name = $this->db->escape($data['name']);
		$sql = "INSERT INTO " . DB_PREFIX . "promotion SET `name` = '" . $name;
		if($data['image']){
			$sql .= "', image = '" . $this->db->escape(html_entity_decode($data['image'], ENT_QUOTES, 'UTF-8'));
		}
		$sql .= "', start_time = '" . (int)$data['start_time'] . 
			"', end_time = '" . (int)$data['end_time'] . 
			"', status = '" . (int)$data['status'] . "'";
		$this->db->query($sql);	
		$promotion_id = $this->db->getLastId();
      	
		foreach ($data['promotion_description'] as $language_id => $value) {
			$sql_desc = "INSERT INTO " . DB_PREFIX . "promotion_description SET promotion_id = '" . (int)$promotion_id . 
				"', language_id = '" . (int)$language_id . 
				"', display_name = '";
			if($value['display_name']){
				$sql_desc .= $this->db->escape($value['display_name']);
			}else{
				$sql_desc .= $name;
			}
			$sql_desc .= "', description = '" . $this->db->escape($value['description']) . "'";
			$this->db->query($sql_desc);
		}
	}
	
	public function editPromotion($promotion_id, $data = array()) {
		$name = $this->db->escape($data['name']);
		$sql = "UPDATE " . DB_PREFIX . "promotion SET `name` = '" . $name;
		if($data['image']){
			$sql .= "', image = '" . $this->db->escape(html_entity_decode($data['image'], ENT_QUOTES, 'UTF-8'));
		}
		$sql .= "', start_time = '" . (int)$data['start_time'] . 
			"', end_time = '" . (int)$data['end_time'] . 
			"', status = '" . (int)$data['status'] . 
		 	"' WHERE promotion_id = '" . (int)$promotion_id . "'";
		$this->db->query($sql);	
      	
		$this->db->query("DELETE FROM ". DB_PREFIX . "promotion_description WHERE promotion_id = '" . (int)$promotion_id . "'");	
		foreach ($data['promotion_description'] as $language_id => $value) {
			$sql_desc = "INSERT INTO " . DB_PREFIX . "promotion_description SET promotion_id = '" . (int)$promotion_id . 
				"', language_id = '" . (int)$language_id . 
				"', display_name = '";
			if($value['display_name']){
				$sql_desc .= $this->db->escape($value['display_name']);
			}else{
				$sql_desc .= $name;
			}
			$sql_desc .= "', description = '" . $this->db->escape($value['description']) . "'";
			$this->db->query($sql_desc);
		}
	}

	public function getPromotion($promotion_id) {
		$query = $this->db->query("SELECT promotion_id, `name`, image, FROM_UNIXTIME(start_time, '%Y-%m-%d') start_time, FROM_UNIXTIME(end_time, '%Y-%m-%d') end_time, status FROM " . 
			DB_PREFIX . "promotion WHERE promotion_id = '" . (int)$promotion_id . "'");
		return $query->row;
	}
	
	public function getPromotionDescription($promotion_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "promotion_description WHERE promotion_id = '" . (int)$promotion_id . "'");
		$promotion_description_data = array();
		foreach ($query->rows as $result) {
			$promotion_description_data[$result['language_id']] = array(
				'promotion_id'     => $result['promotion_id'],
				'language_id'      => $result['language_id'],
				'display_name'     => $result['display_name'],
				'description'      => $result['description']
			);
		}
		return $promotion_description_data;
	}
	
	public function getPromotions($data = array()) {
		$sql = "SELECT p.promotion_id, p.`name`, p.image, FROM_UNIXTIME(p.start_time, '%Y-%m-%d') start_time, " . 
			"FROM_UNIXTIME(p.end_time, '%Y-%m-%d') end_time, p.status, pd.display_name, pd.description FROM " . DB_PREFIX . "promotion p LEFT JOIN " . 
			DB_PREFIX . "promotion_description pd ON p.promotion_id = pd.promotion_id AND pd.language_id = '" . 
			(int)$this->config->get('config_language_id') . "'";

		if (isset($data['status'])) {
			$sql .= " WHERE `status` = '" . (int)$data['status'] . "'";
		} else {
			$sql .= " WHERE `status` = '1'";
		}
		if (isset($data['promotion_id'])) {
			$sql .= " AND promotion_id = '" . (int)$data['promotion_id'] . "'";
		}
		if (isset($data['name'])) {
			$sql .= " AND LCASE(`name`) LIKE '" . $this->db->escape(utf8_strtolower($data['name'])) . "%'";
		}
		if (isset($data['start_time'])) {
			$sql .= " AND start_time >= '" . $this->db->escape($data['start_time']) . "'";
		}
		if (isset($data['end_time'])) {
			$sql .= " AND end_time <= '" . $this->db->escape($data['end_time']) . "'";
		}
		
		$sort_data = array(
			'status',
			'start_time',
			'end_time'
		);
		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];
		} else {
			$sql .= " ORDER BY p.promotion_id";
		}
		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}

		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}
			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}
		$query = $this->db->query($sql);
		return $query->rows;
	}
	
	public function getTotalPromotions($data = array()) {
		$sql = "SELECT COUNT(*) AS total FROM " . DB_PREFIX . "promotion p LEFT JOIN " . 
			DB_PREFIX . "promotion_description pd ON p.promotion_id = pd.promotion_id AND pd.language_id = '" . 
			(int)$this->config->get('config_language_id') . "'";

		if (isset($data['status'])) {
			$sql .= " WHERE `status` = '" . (int)$data['status'] . "'";
		} else {
			$sql .= " WHERE `status` = '1'";
		}
		if (isset($data['promotion_id'])) {
			$sql .= " AND promotion_id = '" . (int)$data['promotion_id'] . "'";
		}
		if (isset($data['name'])) {
			$sql .= " AND LCASE(`name`) LIKE '" . $this->db->escape(utf8_strtolower($data['name'])) . "%'";
		}
		if (isset($data['start_time'])) {
			$sql .= " AND start_time >= '" . $this->db->escape($data['start_time']) . "'";
		}
		if (isset($data['end_time'])) {
			$sql .= " AND end_time <= '" . $this->db->escape($data['end_time']) . "'";
		}
		
		$query = $this->db->query($sql);

		return $query->row['total'];
	}
	
	public function getPromotionsToSelect() {
      	$sql = "SELECT p.promotion_id, p.`name`, FROM_UNIXTIME(p.start_time, '%Y-%m-%d') start_time, " . 
			"FROM_UNIXTIME(p.end_time, '%Y-%m-%d') end_time, pd.display_name FROM `" . DB_PREFIX . "promotion` p LEFT JOIN " . 
      		DB_PREFIX . "promotion_description pd ON p.promotion_id = pd.promotion_id AND pd.language_id = '" . 
      		(int)$this->config->get('config_language_id') . 
      		"' WHERE p.`status` = 1 AND p.start_time <= UNIX_TIMESTAMP() AND p.end_time >= UNIX_TIMESTAMP()";
		$query = $this->db->query($sql);
		return $query->rows;
	}
}
?>